home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / textprop.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  37KB  |  1,265 lines

  1. /* Interface code for dealing with text properties.
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "config.h"
  21. #include "lisp.h"
  22. #include "intervals.h"
  23. #include "buffer.h"
  24.  
  25. #ifdef STDC_HEADERS
  26. #include <stdlib.h>
  27. #endif
  28. #include "textprop_p.h"
  29. #include "insdel_p.h"
  30. static INTERVAL validate_interval_range _P_((Lisp_Object object,
  31.                                              Lisp_Object *begin,
  32.                                              Lisp_Object *end, int force));
  33. static Lisp_Object validate_plist _P_((int list));
  34. static int interval_has_all_properties _P_((Lisp_Object plist, INTERVAL i));
  35. static INLINE int interval_has_some_properties _P_((Lisp_Object plist,
  36.                                                     INTERVAL i));
  37. static int property_value _P_((int plist, int prop));
  38. static void set_properties _P_((Lisp_Object properties, INTERVAL interval,
  39.                                 Lisp_Object object));
  40. static int add_properties _P_((Lisp_Object plist, INTERVAL i,
  41.                                Lisp_Object object));
  42. static int remove_properties _P_((Lisp_Object plist, INTERVAL i,
  43.                                   Lisp_Object object));
  44. static INLINE int erase_properties _P_((INTERVAL i));
  45.  
  46.  
  47. /* NOTES:  previous- and next- property change will have to skip
  48.   zero-length intervals if they are implemented.  This could be done
  49.   inside next_interval and previous_interval.
  50.  
  51.   set_properties needs to deal with the interval property cache.
  52.  
  53.   It is assumed that for any interval plist, a property appears
  54.   only once on the list.  Although some code i.e., remove_properties,
  55.   handles the more general case, the uniqueness of properties is
  56.   necessary for the system to remain consistent.  This requirement
  57.   is enforced by the subrs installing properties onto the intervals. */
  58.  
  59. /* The rest of the file is within this conditional */
  60. #ifdef USE_TEXT_PROPERTIES
  61.  
  62. /* Types of hooks. */
  63. Lisp_Object Qmouse_left;
  64. Lisp_Object Qmouse_entered;
  65. Lisp_Object Qpoint_left;
  66. Lisp_Object Qpoint_entered;
  67. Lisp_Object Qmodification_hooks;
  68. Lisp_Object Qinsert_in_front_hooks;
  69. Lisp_Object Qinsert_behind_hooks;
  70. Lisp_Object Qcategory;
  71. Lisp_Object Qlocal_map;
  72.  
  73. /* Visual properties text (including strings) may have. */
  74. Lisp_Object Qforeground, Qbackground, Qfont, Qunderline, Qstipple;
  75. Lisp_Object Qinvisible, Qread_only;
  76.  
  77. /* If o1 is a cons whose cdr is a cons, return non-zero and set o2 to
  78.    the o1's cdr.  Otherwise, return zero.  This is handy for
  79.    traversing plists.  */
  80. #define PLIST_ELT_P(o1, o2) (CONSP (o1) && CONSP ((o2) = XCONS (o1)->cdr))
  81.  
  82.  
  83. /* Extract the interval at the position pointed to by BEGIN from
  84.    OBJECT, a string or buffer.  Additionally, check that the positions
  85.    pointed to by BEGIN and END are within the bounds of OBJECT, and
  86.    reverse them if *BEGIN is greater than *END.  The objects pointed
  87.    to by BEGIN and END may be integers or markers; if the latter, they
  88.    are coerced to integers.
  89.  
  90.    When OBJECT is a string, we increment *BEGIN and *END
  91.    to make them origin-one.
  92.  
  93.    Note that buffer points don't correspond to interval indices.
  94.    For example, point-max is 1 greater than the index of the last
  95.    character.  This difference is handled in the caller, which uses
  96.    the validated points to determine a length, and operates on that.
  97.    Exceptions are Ftext_properties_at, Fnext_property_change, and
  98.    Fprevious_property_change which call this function with BEGIN == END.
  99.    Handle this case specially.
  100.  
  101.    If FORCE is soft (0), it's OK to return NULL_INTERVAL.  Otherwise,
  102.    create an interval tree for OBJECT if one doesn't exist, provided
  103.    the object actually contains text.  In the current design, if there
  104.    is no text, there can be no text properties.  */
  105.  
  106. #define soft 0
  107. #define hard 1
  108.  
  109. static INTERVAL
  110. validate_interval_range (object, begin, end, force)
  111.      Lisp_Object object, *begin, *end;
  112.      int force;
  113. {
  114.   register INTERVAL i;
  115.   int searchpos;
  116.  
  117.   CHECK_STRING_OR_BUFFER (object, 0);
  118.   CHECK_NUMBER_COERCE_MARKER (*begin, 0);
  119.   CHECK_NUMBER_COERCE_MARKER (*end, 0);
  120.  
  121.   /* If we are asked for a point, but from a subr which operates
  122.      on a range, then return nothing. */
  123.   if (*begin == *end && begin != end)
  124.     return NULL_INTERVAL;
  125.  
  126.   if (XINT (*begin) > XINT (*end))
  127.     {
  128.       Lisp_Object n;
  129.       n = *begin;
  130.       *begin = *end;
  131.       *end = n;
  132.     }
  133.  
  134.   if (XTYPE (object) == Lisp_Buffer)
  135.     {
  136.       register struct buffer *b = XBUFFER (object);
  137.  
  138.       if (!(BUF_BEGV (b) <= XINT (*begin) && XINT (*begin) <= XINT (*end)
  139.         && XINT (*end) <= BUF_ZV (b)))
  140.     args_out_of_range (*begin, *end);
  141.       i = b->intervals;
  142.  
  143.       /* If there's no text, there are no properties. */
  144.       if (BUF_BEGV (b) == BUF_ZV (b))
  145.     return NULL_INTERVAL;
  146.  
  147.       searchpos = XINT (*begin);
  148.     }
  149.   else
  150.     {
  151.       register struct Lisp_String *s = XSTRING (object);
  152.  
  153.       if (! (0 <= XINT (*begin) && XINT (*begin) <= XINT (*end)
  154.          && XINT (*end) <= s->size))
  155.     args_out_of_range (*begin, *end);
  156.       /* User-level Positions in strings start with 0,
  157.      but the interval code always wants positions starting with 1.  */
  158.       XFASTINT (*begin) += 1;
  159.       if (begin != end)
  160.     XFASTINT (*end) += 1;
  161.       i = s->intervals;
  162.  
  163.       if (s->size == 0)
  164.     return NULL_INTERVAL;
  165.  
  166.       searchpos = XINT (*begin);
  167.     }
  168.  
  169.   if (NULL_INTERVAL_P (i))
  170.     return (force ? create_root_interval (object) : i);
  171.     
  172.   return find_interval (i, searchpos);
  173. }
  174.  
  175. /* Validate LIST as a property list.  If LIST is not a list, then
  176.    make one consisting of (LIST nil).  Otherwise, verify that LIST
  177.    is even numbered and thus suitable as a plist. */
  178.  
  179. static Lisp_Object
  180. validate_plist (list)
  181.     int list;
  182. {
  183.   if (NILP (list))
  184.     return Qnil;
  185.  
  186.   if (CONSP (list))
  187.     {
  188.       register int i;
  189.       register Lisp_Object tail;
  190.       for (i = 0, tail = list; !NILP (tail); i++)
  191.     {
  192.       tail = Fcdr (tail);
  193.       QUIT;
  194.     }
  195.       if (i & 1)
  196.     error ("Odd length text property list");
  197.       return list;
  198.     }
  199.  
  200.   return Fcons (list, Fcons (Qnil, Qnil));
  201. }
  202.  
  203. /* Return nonzero if interval I has all the properties,
  204.    with the same values, of list PLIST. */
  205.  
  206. static int
  207. interval_has_all_properties (plist, i)
  208.      Lisp_Object plist;
  209.      INTERVAL i;
  210. {
  211.   register Lisp_Object tail1, tail2, sym1;
  212.   register int found;
  213.  
  214.   /* Go through each element of PLIST. */
  215.   for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
  216.     {
  217.       sym1 = Fcar (tail1);
  218.       found = 0;
  219.  
  220.       /* Go through I's plist, looking for sym1 */
  221.       for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2)))
  222.     if (EQ (sym1, Fcar (tail2)))
  223.       {
  224.         /* Found the same property on both lists.  If the
  225.            values are unequal, return zero. */
  226.         if (! EQ (Fcar (Fcdr (tail1)), Fcar (Fcdr (tail2))))
  227.           return 0;
  228.  
  229.         /* Property has same value on both lists;  go to next one. */
  230.         found = 1;
  231.         break;
  232.       }
  233.  
  234.       if (! found)
  235.     return 0;
  236.     }
  237.  
  238.   return 1;
  239. }
  240.  
  241. /* Return nonzero if the plist of interval I has any of the
  242.    properties of PLIST, regardless of their values. */
  243.  
  244. static INLINE int
  245. interval_has_some_properties (plist, i)
  246.      Lisp_Object plist;
  247.      INTERVAL i;
  248. {
  249.   register Lisp_Object tail1, tail2, sym;
  250.  
  251.   /* Go through each element of PLIST. */
  252.   for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
  253.     {
  254.       sym = Fcar (tail1);
  255.  
  256.       /* Go through i's plist, looking for tail1 */
  257.       for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2)))
  258.     if (EQ (sym, Fcar (tail2)))
  259.       return 1;
  260.     }
  261.  
  262.   return 0;
  263. }
  264.  
  265. /* Changing the plists of individual intervals.  */
  266.  
  267. /* Return the value of PROP in property-list PLIST, or Qunbound if it
  268.    has none.  */
  269. static int
  270. property_value (plist, prop)
  271.     int plist, prop;
  272. {
  273.   Lisp_Object value;
  274.  
  275.   while (PLIST_ELT_P (plist, value))
  276.     if (EQ (XCONS (plist)->car, prop))
  277.       return XCONS (value)->car;
  278.     else
  279.       plist = XCONS (value)->cdr;
  280.  
  281.   return Qunbound;
  282. }
  283.  
  284. /* Set the properties of INTERVAL to PROPERTIES,
  285.    and record undo info for the previous values.
  286.    OBJECT is the string or buffer that INTERVAL belongs to.  */
  287.  
  288. static void
  289. set_properties (properties, interval, object)
  290.      Lisp_Object properties, object;
  291.      INTERVAL interval;
  292. {
  293.   Lisp_Object sym, value;
  294.  
  295.   if (BUFFERP (object))
  296.     {
  297.       /* For each property in the old plist which is missing from PROPERTIES,
  298.      or has a different value in PROPERTIES, make an undo record.  */
  299.       for (sym = interval->plist;
  300.        PLIST_ELT_P (sym, value);
  301.        sym = XCONS (value)->cdr)
  302.     if (! EQ (property_value (properties, XCONS (sym)->car),
  303.           XCONS (value)->car))
  304.       {
  305.         modify_region (XBUFFER (object),
  306.                make_number (interval->position),
  307.                make_number (interval->position + LENGTH (interval)));
  308.         record_property_change (interval->position, LENGTH (interval),
  309.                     XCONS (sym)->car, XCONS (value)->car,
  310.                     object);
  311.       }
  312.  
  313.       /* For each new property that has no value at all in the old plist,
  314.      make an undo record binding it to nil, so it will be removed.  */
  315.       for (sym = properties;
  316.        PLIST_ELT_P (sym, value);
  317.        sym = XCONS (value)->cdr)
  318.     if (EQ (property_value (interval->plist, XCONS (sym)->car), Qunbound))
  319.       {
  320.         modify_region (XBUFFER (object),
  321.                make_number (interval->position),
  322.                make_number (interval->position + LENGTH (interval)));
  323.         record_property_change (interval->position, LENGTH (interval),
  324.                     XCONS (sym)->car, Qnil,
  325.                     object);
  326.       }
  327.     }
  328.  
  329.   /* Store new properties.  */
  330.   interval->plist = Fcopy_sequence (properties);
  331. }
  332.  
  333. /* Add the properties of PLIST to the interval I, or set
  334.    the value of I's property to the value of the property on PLIST
  335.    if they are different.
  336.  
  337.    OBJECT should be the string or buffer the interval is in.
  338.  
  339.    Return nonzero if this changes I (i.e., if any members of PLIST
  340.    are actually added to I's plist) */
  341.  
  342. static int
  343. add_properties (plist, i, object)
  344.      Lisp_Object plist;
  345.      INTERVAL i;
  346.      Lisp_Object object;
  347. {
  348.   register Lisp_Object tail1, tail2, sym1, val1;
  349.   register int changed = 0;
  350.   register int found;
  351.  
  352.   /* Go through each element of PLIST. */
  353.   for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
  354.     {
  355.       sym1 = Fcar (tail1);
  356.       val1 = Fcar (Fcdr (tail1));
  357.       found = 0;
  358.  
  359.       /* Go through I's plist, looking for sym1 */
  360.       for (tail2 = i->plist; ! NILP (tail2); tail2 = Fcdr (Fcdr (tail2)))
  361.     if (EQ (sym1, Fcar (tail2)))
  362.       {
  363.         register Lisp_Object this_cdr = Fcdr (tail2);
  364.  
  365.         /* Found the property.  Now check its value. */
  366.         found = 1;
  367.  
  368.         /* The properties have the same value on both lists.
  369.            Continue to the next property. */
  370.         if (EQ (val1, Fcar (this_cdr)))
  371.           break;
  372.  
  373.         /* Record this change in the buffer, for undo purposes.  */
  374.         if (XTYPE (object) == Lisp_Buffer)
  375.           {
  376.         modify_region (XBUFFER (object),
  377.                    make_number (i->position),
  378.                    make_number (i->position + LENGTH (i)));
  379.         record_property_change (i->position, LENGTH (i),
  380.                     sym1, Fcar (this_cdr), object);
  381.           }
  382.  
  383.         /* I's property has a different value -- change it */
  384.         Fsetcar (this_cdr, val1);
  385.         changed++;
  386.         break;
  387.       }
  388.  
  389.       if (! found)
  390.     {
  391.       /* Record this change in the buffer, for undo purposes.  */
  392.       if (XTYPE (object) == Lisp_Buffer)
  393.         {
  394.           modify_region (XBUFFER (object),
  395.                  make_number (i->position),
  396.                  make_number (i->position + LENGTH (i)));
  397.           record_property_change (i->position, LENGTH (i),
  398.                       sym1, Qnil, object);
  399.         }
  400.       i->plist = Fcons (sym1, Fcons (val1, i->plist));
  401.       changed++;
  402.     }
  403.     }
  404.  
  405.   return changed;
  406. }
  407.  
  408. /* For any members of PLIST which are properties of I, remove them
  409.    from I's plist.
  410.    OBJECT is the string or buffer containing I.  */
  411.  
  412. static int
  413. remove_properties (plist, i, object)
  414.      Lisp_Object plist;
  415.      INTERVAL i;
  416.      Lisp_Object object;
  417. {
  418.   register Lisp_Object tail1, tail2, sym;
  419.   register Lisp_Object current_plist = i->plist;
  420.   register int changed = 0;
  421.  
  422.   /* Go through each element of plist. */
  423.   for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1)))
  424.     {
  425.       sym = Fcar (tail1);
  426.  
  427.       /* First, remove the symbol if its at the head of the list */
  428.       while (! NILP (current_plist) && EQ (sym, Fcar (current_plist)))
  429.     {
  430.       if (XTYPE (object) == Lisp_Buffer)
  431.         {
  432.           modify_region (XBUFFER (object),
  433.                  make_number (i->position),
  434.                  make_number (i->position + LENGTH (i)));
  435.           record_property_change (i->position, LENGTH (i),
  436.                       sym, Fcar (Fcdr (current_plist)),
  437.                       object);
  438.         }
  439.  
  440.       current_plist = Fcdr (Fcdr (current_plist));
  441.       changed++;
  442.     }
  443.  
  444.       /* Go through i's plist, looking for sym */
  445.       tail2 = current_plist;
  446.       while (! NILP (tail2))
  447.     {
  448.       register Lisp_Object this = Fcdr (Fcdr (tail2));
  449.       if (EQ (sym, Fcar (this)))
  450.         {
  451.           if (XTYPE (object) == Lisp_Buffer)
  452.         {
  453.           modify_region (XBUFFER (object),
  454.                  make_number (i->position),
  455.                  make_number (i->position + LENGTH (i)));
  456.           record_property_change (i->position, LENGTH (i),
  457.                       sym, Fcar (Fcdr (this)), object);
  458.         }
  459.  
  460.           Fsetcdr (Fcdr (tail2), Fcdr (Fcdr (this)));
  461.           changed++;
  462.         }
  463.       tail2 = this;
  464.     }
  465.     }
  466.  
  467.   if (changed)
  468.     i->plist = current_plist;
  469.   return changed;
  470. }
  471.  
  472. #if 0
  473. /* Remove all properties from interval I.  Return non-zero
  474.    if this changes the interval. */
  475.  
  476. static INLINE int
  477. erase_properties (i)
  478.      INTERVAL i;
  479. {
  480.   if (NILP (i->plist))
  481.     return 0;
  482.  
  483.   i->plist = Qnil;
  484.   return 1;
  485. }
  486. #endif
  487.  
  488. DEFUN ("text-properties-at", Ftext_properties_at,
  489.        Stext_properties_at, 1, 2, 0,
  490.   "Return the list of properties held by the character at POSITION\n\
  491. in optional argument OBJECT, a string or buffer.  If nil, OBJECT\n\
  492. defaults to the current buffer.\n\
  493. If POSITION is at the end of OBJECT, the value is nil.")
  494.   (pos, object)
  495.      Lisp_Object pos, object;
  496. {
  497.   register INTERVAL i;
  498.  
  499.   if (NILP (object))
  500.     XSET (object, Lisp_Buffer, current_buffer);
  501.  
  502.   i = validate_interval_range (object, &pos, &pos, soft);
  503.   if (NULL_INTERVAL_P (i))
  504.     return Qnil;
  505.   /* If POS is at the end of the interval,
  506.      it means it's the end of OBJECT.
  507.      There are no properties at the very end,
  508.      since no character follows.  */
  509.   if ((unsigned)XINT (pos) == LENGTH (i) + i->position)
  510.     return Qnil;
  511.  
  512.   return i->plist;
  513. }
  514.  
  515. DEFUN ("get-text-property", Fget_text_property, Sget_text_property, 2, 3, 0,
  516.   "Return the value of position POS's property PROP, in OBJECT.\n\
  517. OBJECT is optional and defaults to the current buffer.\n\
  518. If POSITION is at the end of OBJECT, the value is nil.")
  519.   (pos, prop, object)
  520.      Lisp_Object pos, object;
  521.      register Lisp_Object prop;
  522. {
  523.   register INTERVAL i;
  524.  
  525.   if (NILP (object))
  526.     XSET (object, Lisp_Buffer, current_buffer);
  527.   i = validate_interval_range (object, &pos, &pos, soft);
  528.   if (NULL_INTERVAL_P (i))
  529.     return Qnil;
  530.  
  531.   /* If POS is at the end of the interval,
  532.      it means it's the end of OBJECT.
  533.      There are no properties at the very end,
  534.      since no character follows.  */
  535.   if ((unsigned)XINT (pos) == LENGTH (i) + i->position)
  536.     return Qnil;
  537.  
  538.   return textget (i->plist, prop);
  539. }
  540.  
  541. DEFUN ("next-property-change", Fnext_property_change,
  542.        Snext_property_change, 1, 2, 0,
  543.   "Return the position of next property change.\n\
  544. Scans characters forward from POS in OBJECT till it finds\n\
  545. a change in some text property, then returns the position of the change.\n\
  546. The optional second argument OBJECT is the string or buffer to scan.\n\
  547. Return nil if the property is constant all the way to the end of OBJECT.\n\
  548. If the value is non-nil, it is a position greater than POS, never equal.")
  549.   (pos, object)
  550.      Lisp_Object pos, object;
  551. {
  552.   register INTERVAL i, next;
  553.  
  554.   if (NILP (object))
  555.     XSET (object, Lisp_Buffer, current_buffer);
  556.  
  557.   i = validate_interval_range (object, &pos, &pos, soft);
  558.   if (NULL_INTERVAL_P (i))
  559.     return Qnil;
  560.  
  561.   next = next_interval (i);
  562.   while (! NULL_INTERVAL_P (next) && intervals_equal (i, next))
  563.     next = next_interval (next);
  564.  
  565.   if (NULL_INTERVAL_P (next))
  566.     return Qnil;
  567.  
  568.   return next->position - (XTYPE (object) == Lisp_String);
  569. ;
  570. }
  571.  
  572. DEFUN ("next-single-property-change", Fnext_single_property_change,
  573.        Snext_single_property_change, 1, 3, 0,
  574.   "Return the position of next property change for a specific property.\n\
  575. Scans characters forward from POS till it finds\n\
  576. a change in the PROP property, then returns the position of the change.\n\
  577. The optional third argument OBJECT is the string or buffer to scan.\n\
  578. Return nil if the property is constant all the way to the end of OBJECT.\n\
  579. If the value is non-nil, it is a position greater than POS, never equal.")
  580.   (pos, prop, object)
  581.      Lisp_Object pos, prop, object;
  582. {
  583.   register INTERVAL i, next;
  584.   register Lisp_Object here_val;
  585.  
  586.   if (NILP (object))
  587.     XSET (object, Lisp_Buffer, current_buffer);
  588.  
  589.   i = validate_interval_range (object, &pos, &pos, soft);
  590.   if (NULL_INTERVAL_P (i))
  591.     return Qnil;
  592.  
  593.   here_val = textget (i->plist, prop);
  594.   next = next_interval (i);
  595.   while (! NULL_INTERVAL_P (next) 
  596.      && EQ (here_val, textget (next->plist, prop)))
  597.     next = next_interval (next);
  598.  
  599.   if (NULL_INTERVAL_P (next))
  600.     return Qnil;
  601.  
  602.   return next->position - (XTYPE (object) == Lisp_String);
  603. }
  604.  
  605. DEFUN ("previous-property-change", Fprevious_property_change,
  606.        Sprevious_property_change, 1, 2, 0,
  607.   "Return the position of previous property change.\n\
  608. Scans characters backwards from POS in OBJECT till it finds\n\
  609. a change in some text property, then returns the position of the change.\n\
  610. The optional second argument OBJECT is the string or buffer to scan.\n\
  611. Return nil if the property is constant all the way to the start of OBJECT.\n\
  612. If the value is non-nil, it is a position less than POS, never equal.")
  613.   (pos, object)
  614.      Lisp_Object pos, object;
  615. {
  616.   register INTERVAL i, previous;
  617.  
  618.   if (NILP (object))
  619.     XSET (object, Lisp_Buffer, current_buffer);
  620.  
  621.   i = validate_interval_range (object, &pos, &pos, soft);
  622.   if (NULL_INTERVAL_P (i))
  623.     return Qnil;
  624.  
  625.   previous = previous_interval (i);
  626.   while (! NULL_INTERVAL_P (previous) && intervals_equal (previous, i))
  627.     previous = previous_interval (previous);
  628.   if (NULL_INTERVAL_P (previous))
  629.     return Qnil;
  630.  
  631.   return (previous->position + LENGTH (previous) - 1
  632.       - (XTYPE (object) == Lisp_String));
  633. }
  634.  
  635. DEFUN ("previous-single-property-change", Fprevious_single_property_change,
  636.        Sprevious_single_property_change, 2, 3, 0,
  637.   "Return the position of previous property change for a specific property.\n\
  638. Scans characters backward from POS till it finds\n\
  639. a change in the PROP property, then returns the position of the change.\n\
  640. The optional third argument OBJECT is the string or buffer to scan.\n\
  641. Return nil if the property is constant all the way to the start of OBJECT.\n\
  642. If the value is non-nil, it is a position less than POS, never equal.")
  643.      (pos, prop, object)
  644.      Lisp_Object pos, prop, object;
  645. {
  646.   register INTERVAL i, previous;
  647.   register Lisp_Object here_val;
  648.  
  649.   if (NILP (object))
  650.     XSET (object, Lisp_Buffer, current_buffer);
  651.  
  652.   i = validate_interval_range (object, &pos, &pos, soft);
  653.   if (NULL_INTERVAL_P (i))
  654.     return Qnil;
  655.  
  656.   here_val = textget (i->plist, prop);
  657.   previous = previous_interval (i);
  658.   while (! NULL_INTERVAL_P (previous)
  659.      && EQ (here_val, textget (previous->plist, prop)))
  660.     previous = previous_interval (previous);
  661.   if (NULL_INTERVAL_P (previous))
  662.     return Qnil;
  663.  
  664.   return (previous->position + LENGTH (previous) - 1
  665.       - (XTYPE (object) == Lisp_String));
  666. }
  667.  
  668. DEFUN ("add-text-properties", Fadd_text_properties,
  669.        Sadd_text_properties, 3, 4, 0,
  670.   "Add properties to the text from START to END.\n\
  671. The third argument PROPS is a property list\n\
  672. specifying the property values to add.\n\
  673. The optional fourth argument, OBJECT,\n\
  674. is the string or buffer containing the text.\n\
  675. Return t if any property value actually changed, nil otherwise.")
  676.   (start, end, properties, object)
  677.      Lisp_Object start, end, properties, object;
  678. {
  679.   register INTERVAL i, unchanged;
  680.   register int s, len, modified = 0;
  681.  
  682.   properties = validate_plist (properties);
  683.   if (NILP (properties))
  684.     return Qnil;
  685.  
  686.   if (NILP (object))
  687.     XSET (object, Lisp_Buffer, current_buffer);
  688.  
  689.   i = validate_interval_range (object, &start, &end, hard);
  690.   if (NULL_INTERVAL_P (i))
  691.     return Qnil;
  692.  
  693.   s = XINT (start);
  694.   len = XINT (end) - s;
  695.  
  696.   /* If we're not starting on an interval boundary, we have to
  697.     split this interval. */
  698.   if (i->position != (unsigned)s)
  699.     {
  700.       /* If this interval already has the properties, we can
  701.          skip it. */
  702.       if (interval_has_all_properties (properties, i))
  703.     {
  704.       int got = (LENGTH (i) - (s - i->position));
  705.       if (got >= len)
  706.         return Qnil;
  707.       len -= got;
  708.       i = next_interval (i);
  709.     }
  710.       else
  711.     {
  712.       unchanged = i;
  713.       i = split_interval_right (unchanged, s - unchanged->position);
  714.       copy_properties (unchanged, i);
  715.     }
  716.     }
  717.  
  718.   /* We are at the beginning of interval I, with LEN chars to scan.  */
  719.   for (;;)
  720.     {
  721.       if (i == 0)
  722.     abort ();
  723.  
  724.       if (LENGTH (i) >= (unsigned)len)
  725.     {
  726.       if (interval_has_all_properties (properties, i))
  727.         return modified ? Qt : Qnil;
  728.  
  729.       if (LENGTH (i) == (unsigned)len)
  730.         {
  731.           add_properties (properties, i, object);
  732.           return Qt;
  733.         }
  734.  
  735.       /* i doesn't have the properties, and goes past the change limit */
  736.       unchanged = i;
  737.       i = split_interval_left (unchanged, len);
  738.       copy_properties (unchanged, i);
  739.       add_properties (properties, i, object);
  740.       return Qt;
  741.     }
  742.  
  743.       len -= LENGTH (i);
  744.       modified += add_properties (properties, i, object);
  745.       i = next_interval (i);
  746.     }
  747. }
  748.  
  749. DEFUN ("put-text-property", Fput_text_property,
  750.        Sput_text_property, 4, 5, 0,
  751.   "Set one property of the text from START to END.\n\
  752. The third and fourth arguments PROP and VALUE\n\
  753. specify the property to add.\n\
  754. The optional fifth argument, OBJECT,\n\
  755. is the string or buffer containing the text.")
  756.   (start, end, prop, value, object)
  757.      Lisp_Object start, end, prop, value, object;
  758. {
  759.   Fadd_text_properties (start, end,
  760.             Fcons (prop, Fcons (value, Qnil)),
  761.             object);
  762.   return Qnil;
  763. }
  764.  
  765. DEFUN ("set-text-properties", Fset_text_properties,
  766.        Sset_text_properties, 3, 4, 0,
  767.   "Completely replace properties of text from START to END.\n\
  768. The third argument PROPS is the new property list.\n\
  769. The optional fourth argument, OBJECT,\n\
  770. is the string or buffer containing the text.")
  771.   (start, end, props, object)
  772.      Lisp_Object start, end, props, object;
  773. {
  774.   register INTERVAL i, unchanged;
  775.   register INTERVAL prev_changed = NULL_INTERVAL;
  776.   register int s, len;
  777.  
  778.   props = validate_plist (props);
  779.  
  780.   if (NILP (object))
  781.     XSET (object, Lisp_Buffer, current_buffer);
  782.  
  783.   i = validate_interval_range (object, &start, &end, hard);
  784.   if (NULL_INTERVAL_P (i))
  785.     return Qnil;
  786.  
  787.   s = XINT (start);
  788.   len = XINT (end) - s;
  789.  
  790.   if (i->position != (unsigned)s)
  791.     {
  792.       unchanged = i;
  793.       i = split_interval_right (unchanged, s - unchanged->position);
  794.  
  795.       if (LENGTH (i) > (unsigned)len)
  796.     {
  797.       copy_properties (unchanged, i);
  798.       i = split_interval_left (i, len);
  799.       set_properties (props, i, object);
  800.       return Qt;
  801.     }
  802.  
  803.       set_properties (props, i, object);
  804.  
  805.       if (LENGTH (i) == (unsigned)len)
  806.     return Qt;
  807.  
  808.       prev_changed = i;
  809.       len -= LENGTH (i);
  810.       i = next_interval (i);
  811.     }
  812.  
  813.   /* We are starting at the beginning of an interval, I */
  814.   while (len > 0)
  815.     {
  816.       if (i == 0)
  817.     abort ();
  818.  
  819.       if (LENGTH (i) >= (unsigned)len)
  820.     {
  821.       if (LENGTH (i) > (unsigned)len)
  822.         i = split_interval_left (i, len);
  823.  
  824.       if (NULL_INTERVAL_P (prev_changed))
  825.         set_properties (props, i, object);
  826.       else
  827.         merge_interval_left (i);
  828.       return Qt;
  829.     }
  830.  
  831.       len -= LENGTH (i);
  832.       if (NULL_INTERVAL_P (prev_changed))
  833.     {
  834.       set_properties (props, i, object);
  835.       prev_changed = i;
  836.     }
  837.       else
  838.     prev_changed = i = merge_interval_left (i);
  839.  
  840.       i = next_interval (i);
  841.     }
  842.  
  843.   return Qt;
  844. }
  845.  
  846. DEFUN ("remove-text-properties", Fremove_text_properties,
  847.        Sremove_text_properties, 3, 4, 0,
  848.   "Remove some properties from text from START to END.\n\
  849. The third argument PROPS is a property list\n\
  850. whose property names specify the properties to remove.\n\
  851. \(The values stored in PROPS are ignored.)\n\
  852. The optional fourth argument, OBJECT,\n\
  853. is the string or buffer containing the text.\n\
  854. Return t if any property was actually removed, nil otherwise.")
  855.   (start, end, props, object)
  856.      Lisp_Object start, end, props, object;
  857. {
  858.   register INTERVAL i, unchanged;
  859.   register int s, len, modified = 0;
  860.  
  861.   if (NILP (object))
  862.     XSET (object, Lisp_Buffer, current_buffer);
  863.  
  864.   i = validate_interval_range (object, &start, &end, soft);
  865.   if (NULL_INTERVAL_P (i))
  866.     return Qnil;
  867.  
  868.   s = XINT (start);
  869.   len = XINT (end) - s;
  870.  
  871.   if (i->position != (unsigned)s)
  872.     {
  873.       /* No properties on this first interval -- return if
  874.          it covers the entire region. */
  875.       if (! interval_has_some_properties (props, i))
  876.     {
  877.       int got = (LENGTH (i) - (s - i->position));
  878.       if (got >= len)
  879.         return Qnil;
  880.       len -= got;
  881.       i = next_interval (i);
  882.     }
  883.       /* Split away the beginning of this interval; what we don't
  884.      want to modify.  */
  885.       else
  886.     {
  887.       unchanged = i;
  888.       i = split_interval_right (unchanged, s - unchanged->position);
  889.       copy_properties (unchanged, i);
  890.     }
  891.     }
  892.  
  893.   /* We are at the beginning of an interval, with len to scan */
  894.   for (;;)
  895.     {
  896.       if (i == 0)
  897.     abort ();
  898.  
  899.       if (LENGTH (i) >= (unsigned)len)
  900.     {
  901.       if (! interval_has_some_properties (props, i))
  902.         return modified ? Qt : Qnil;
  903.  
  904.       if (LENGTH (i) == (unsigned)len)
  905.         {
  906.           remove_properties (props, i, object);
  907.           return Qt;
  908.         }
  909.  
  910.       /* i has the properties, and goes past the change limit */
  911.       unchanged = i;
  912.       i = split_interval_left (i, len);
  913.       copy_properties (unchanged, i);
  914.       remove_properties (props, i, object);
  915.       return Qt;
  916.     }
  917.  
  918.       len -= LENGTH (i);
  919.       modified += remove_properties (props, i, object);
  920.       i = next_interval (i);
  921.     }
  922. }
  923.  
  924. DEFUN ("text-property-any", Ftext_property_any,
  925.        Stext_property_any, 4, 5, 0,
  926.   "Check text from START to END to see if PROP is ever `eq' to VALUE.\n\
  927. If so, return the position of the first character whose PROP is `eq'\n\
  928. to VALUE.  Otherwise return nil.\n\
  929. The optional fifth argument, OBJECT, is the string or buffer\n\
  930. containing the text.")
  931.   (start, end, prop, value, object)
  932.        Lisp_Object start, end, prop, value, object;
  933. {
  934.   register INTERVAL i;
  935.   register int e, pos;
  936.  
  937.   if (NILP (object))
  938.     XSET (object, Lisp_Buffer, current_buffer);
  939.   i = validate_interval_range (object, &start, &end, soft);
  940.   e = XINT (end);
  941.  
  942.   while (! NULL_INTERVAL_P (i))
  943.     {
  944.       if (i->position >= (unsigned)e)
  945.     break;
  946.       if (EQ (textget (i->plist, prop), value))
  947.     {
  948.       pos = i->position;
  949.       if (pos < XINT (start))
  950.         pos = XINT (start);
  951.       return make_number (pos - (XTYPE (object) == Lisp_String));
  952.     }
  953.       i = next_interval (i);
  954.     }
  955.   return Qnil;
  956. }
  957.  
  958. DEFUN ("text-property-not-all", Ftext_property_not_all,
  959.        Stext_property_not_all, 4, 5, 0,
  960.   "Check text from START to END to see if PROP is ever not `eq' to VALUE.\n\
  961. If so, return the position of the first character whose PROP is not\n\
  962. `eq' to VALUE.  Otherwise, return nil.\n\
  963. The optional fifth argument, OBJECT, is the string or buffer\n\
  964. containing the text.")
  965.   (start, end, prop, value, object)
  966.        Lisp_Object start, end, prop, value, object;
  967. {
  968.   register INTERVAL i;
  969.   register int s, e;
  970.  
  971.   if (NILP (object))
  972.     XSET (object, Lisp_Buffer, current_buffer);
  973.   i = validate_interval_range (object, &start, &end, soft);
  974.   if (NULL_INTERVAL_P (i))
  975.     return (NILP (value) || EQ (start, end)) ? Qt : Qnil;
  976.   s = XINT (start);
  977.   e = XINT (end);
  978.  
  979.   while (! NULL_INTERVAL_P (i))
  980.     {
  981.       if (i->position >= (unsigned)e)
  982.     break;
  983.       if (! EQ (textget (i->plist, prop), value))
  984.     {
  985.       if (i->position > (unsigned)s)
  986.         s = i->position;
  987.       return make_number (s - (XTYPE (object) == Lisp_String));
  988.     }
  989.       i = next_interval (i);
  990.     }
  991.   return Qnil;
  992. }
  993.  
  994. #if 0 /* You can use set-text-properties for this.  */
  995.  
  996. DEFUN ("erase-text-properties", Ferase_text_properties,
  997.        Serase_text_properties, 2, 3, 0,
  998.   "Remove all properties from the text from START to END.\n\
  999. The optional third argument, OBJECT,\n\
  1000. is the string or buffer containing the text.")
  1001.   (start, end, object)
  1002.      Lisp_Object start, end, object;
  1003. {
  1004.   register INTERVAL i;
  1005.   register INTERVAL prev_changed = NULL_INTERVAL;
  1006.   register int s, len, modified;
  1007.  
  1008.   if (NILP (object))
  1009.     XSET (object, Lisp_Buffer, current_buffer);
  1010.  
  1011.   i = validate_interval_range (object, &start, &end, soft);
  1012.   if (NULL_INTERVAL_P (i))
  1013.     return Qnil;
  1014.  
  1015.   s = XINT (start);
  1016.   len = XINT (end) - s;
  1017.  
  1018.   if (i->position != s)
  1019.     {
  1020.       register int got;
  1021.       register INTERVAL unchanged = i;
  1022.  
  1023.       /* If there are properties here, then this text will be modified. */
  1024.       if (! NILP (i->plist))
  1025.     {
  1026.       i = split_interval_right (unchanged, s - unchanged->position);
  1027.       i->plist = Qnil;
  1028.       modified++;
  1029.  
  1030.       if (LENGTH (i) > len)
  1031.         {
  1032.           i = split_interval_right (i, len);
  1033.           copy_properties (unchanged, i);
  1034.           return Qt;
  1035.         }
  1036.  
  1037.       if (LENGTH (i) == len)
  1038.         return Qt;
  1039.  
  1040.       got = LENGTH (i);
  1041.     }
  1042.       /* If the text of I is without any properties, and contains
  1043.          LEN or more characters, then we may return without changing
  1044.      anything.*/
  1045.       else if (LENGTH (i) - (s - i->position) <= len)
  1046.     return Qnil;
  1047.       /* The amount of text to change extends past I, so just note
  1048.      how much we've gotten. */
  1049.       else
  1050.     got = LENGTH (i) - (s - i->position);
  1051.  
  1052.       len -= got;
  1053.       prev_changed = i;
  1054.       i = next_interval (i);
  1055.     }
  1056.  
  1057.   /* We are starting at the beginning of an interval, I. */
  1058.   while (len > 0)
  1059.     {
  1060.       if (LENGTH (i) >= len)
  1061.     {
  1062.       /* If I has no properties, simply merge it if possible.  */
  1063.       if (NILP (i->plist))
  1064.         {
  1065.           if (! NULL_INTERVAL_P (prev_changed))
  1066.         merge_interval_left (i);
  1067.  
  1068.           return modified ? Qt : Qnil;
  1069.         }
  1070.  
  1071.           if (LENGTH (i) > len)
  1072.             i = split_interval_left (i, len);
  1073.       if (! NULL_INTERVAL_P (prev_changed))
  1074.         merge_interval_left (i);
  1075.       else
  1076.         i->plist = Qnil;
  1077.  
  1078.       return Qt;
  1079.     }
  1080.  
  1081.       /* Here if we still need to erase past the end of I */
  1082.       len -= LENGTH (i);
  1083.       if (NULL_INTERVAL_P (prev_changed))
  1084.     {
  1085.       modified += erase_properties (i);
  1086.       prev_changed = i;
  1087.     }
  1088.       else
  1089.     {
  1090.       modified += ! NILP (i->plist);
  1091.       /* Merging I will give it the properties of PREV_CHANGED. */
  1092.       prev_changed = i = merge_interval_left (i);
  1093.     }
  1094.  
  1095.       i = next_interval (i);
  1096.     }
  1097.  
  1098.   return modified ? Qt : Qnil;
  1099. }
  1100. #endif /* 0 */
  1101.  
  1102. /* I don't think this is the right interface to export; how often do you
  1103.    want to do something like this, other than when you're copying objects
  1104.    around?
  1105.  
  1106.    I think it would be better to have a pair of functions, one which
  1107.    returns the text properties of a region as a list of ranges and
  1108.    plists, and another which applies such a list to another object.  */
  1109.  
  1110. /* DEFUN ("copy-text-properties", Fcopy_text_properties,
  1111.        Scopy_text_properties, 5, 6, 0,
  1112.   "Add properties from SRC-START to SRC-END of SRC at DEST-POS of DEST.\n\
  1113. SRC and DEST may each refer to strings or buffers.\n\
  1114. Optional sixth argument PROP causes only that property to be copied.\n\
  1115. Properties are copied to DEST as if by `add-text-properties'.\n\
  1116. Return t if any property value actually changed, nil otherwise.") */
  1117.  
  1118. Lisp_Object
  1119. copy_text_properties (start, end, src, pos, dest, prop)
  1120.        Lisp_Object start, end, src, pos, dest, prop;
  1121. {
  1122.   INTERVAL i;
  1123.   Lisp_Object res;
  1124.   Lisp_Object stuff;
  1125.   Lisp_Object plist;
  1126.   int s, e, e2, p, len, modified = 0;
  1127.  
  1128.   i = validate_interval_range (src, &start, &end, soft);
  1129.   if (NULL_INTERVAL_P (i))
  1130.     return Qnil;
  1131.  
  1132.   CHECK_NUMBER_COERCE_MARKER (pos, 0);
  1133.   {
  1134.     Lisp_Object dest_start, dest_end;
  1135.  
  1136.     dest_start = pos;
  1137.     XFASTINT (dest_end) = XINT (dest_start) + (XINT (end) - XINT (start));
  1138.     /* Apply this to a copy of pos; it will try to increment its arguments,
  1139.        which we don't want.  */
  1140.     validate_interval_range (dest, &dest_start, &dest_end, soft);
  1141.   }
  1142.  
  1143.   s = XINT (start);
  1144.   e = XINT (end);
  1145.   p = XINT (pos);
  1146.  
  1147.   stuff = Qnil;
  1148.  
  1149.   while (s < e)
  1150.     {
  1151.       e2 = i->position + LENGTH (i);
  1152.       if (e2 > e)
  1153.     e2 = e;
  1154.       len = e2 - s;
  1155.  
  1156.       plist = i->plist;
  1157.       if (! NILP (prop))
  1158.     while (! NILP (plist))
  1159.       {
  1160.         if (EQ (Fcar (plist), prop))
  1161.           {
  1162.         plist = Fcons (prop, Fcons (Fcar (Fcdr (plist)), Qnil));
  1163.         break;
  1164.           }
  1165.         plist = Fcdr (Fcdr (plist));
  1166.       }
  1167.       if (! NILP (plist))
  1168.     {
  1169.       /* Must defer modifications to the interval tree in case src
  1170.          and dest refer to the same string or buffer. */
  1171.       stuff = Fcons (Fcons (make_number (p),
  1172.                 Fcons (make_number (p + len),
  1173.                        Fcons (plist, Qnil))),
  1174.             stuff);
  1175.     }
  1176.  
  1177.       i = next_interval (i);
  1178.       if (NULL_INTERVAL_P (i))
  1179.     break;
  1180.  
  1181.       p += len;
  1182.       s = i->position;
  1183.     }
  1184.  
  1185.   while (! NILP (stuff))
  1186.     {
  1187.       res = Fcar (stuff);
  1188.       res = Fadd_text_properties (Fcar (res), Fcar (Fcdr (res)),
  1189.                   Fcar (Fcdr (Fcdr (res))), dest);
  1190.       if (! NILP (res))
  1191.     modified++;
  1192.       stuff = Fcdr (stuff);
  1193.     }
  1194.  
  1195.   return modified ? Qt : Qnil;
  1196. }
  1197.  
  1198. void
  1199. syms_of_textprop ()
  1200. {
  1201.   DEFVAR_INT ("interval-balance-threshold", &interval_balance_threshold,
  1202.           "Threshold for rebalancing interval trees, expressed as the\n\
  1203. percentage by which the left interval tree should not differ from the right.");
  1204.   interval_balance_threshold = 8;
  1205.  
  1206.   /* Common attributes one might give text */
  1207.  
  1208.   staticpro (&Qforeground);
  1209.   Qforeground = intern ("foreground");
  1210.   staticpro (&Qbackground);
  1211.   Qbackground = intern ("background");
  1212.   staticpro (&Qfont);
  1213.   Qfont = intern ("font");
  1214.   staticpro (&Qstipple);
  1215.   Qstipple = intern ("stipple");
  1216.   staticpro (&Qunderline);
  1217.   Qunderline = intern ("underline");
  1218.   staticpro (&Qread_only);
  1219.   Qread_only = intern ("read-only");
  1220.   staticpro (&Qinvisible);
  1221.   Qinvisible = intern ("invisible");
  1222.   staticpro (&Qcategory);
  1223.   Qcategory = intern ("category");
  1224.   staticpro (&Qlocal_map);
  1225.   Qlocal_map = intern ("local-map");
  1226.  
  1227.   /* Properties that text might use to specify certain actions */
  1228.  
  1229.   staticpro (&Qmouse_left);
  1230.   Qmouse_left = intern ("mouse-left");
  1231.   staticpro (&Qmouse_entered);
  1232.   Qmouse_entered = intern ("mouse-entered");
  1233.   staticpro (&Qpoint_left);
  1234.   Qpoint_left = intern ("point-left");
  1235.   staticpro (&Qpoint_entered);
  1236.   Qpoint_entered = intern ("point-entered");
  1237.   staticpro (&Qmodification_hooks);
  1238.   Qmodification_hooks = intern ("modification-hooks");
  1239.   staticpro (&Qinsert_in_front_hooks);
  1240.   Qinsert_in_front_hooks = intern ("insert-in-front-hooks");
  1241.   staticpro (&Qinsert_behind_hooks);
  1242.   Qinsert_behind_hooks = intern ("insert-behind-hooks");
  1243.  
  1244.   defsubr (&Stext_properties_at);
  1245.   defsubr (&Sget_text_property);
  1246.   defsubr (&Snext_property_change);
  1247.   defsubr (&Snext_single_property_change);
  1248.   defsubr (&Sprevious_property_change);
  1249.   defsubr (&Sprevious_single_property_change);
  1250.   defsubr (&Sadd_text_properties);
  1251.   defsubr (&Sput_text_property);
  1252.   defsubr (&Sset_text_properties);
  1253.   defsubr (&Sremove_text_properties);
  1254.   defsubr (&Stext_property_any);
  1255.   defsubr (&Stext_property_not_all);
  1256. /*  defsubr (&Serase_text_properties); */
  1257. /*  defsubr (&Scopy_text_properties); */
  1258. }
  1259.  
  1260. #else
  1261.  
  1262. lose -- this shouldn't be compiled if USE_TEXT_PROPERTIES isn't defined
  1263.  
  1264. #endif /* USE_TEXT_PROPERTIES */
  1265.